package api

import (
	"gitee.com/hust-ideas/little-genius-backend/model"
	"gitee.com/hust-ideas/little-genius-backend/service"
	"github.com/gin-gonic/gin"
)

// AuthRegisterPost - 用户注册
func AuthRegisterPost(c *gin.Context) {
	s := c.MustGet("service").(*service.ServiceContext)
	// 获取请求 json
	req := model.AuthRegisterReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}
	// 调用 service
	resp, err := s.AuthRegisterPost(req, c.Request.Context())
	if err != nil {
		if err == model.ErrUserEmailAlreadyExists {
			c.JSON(409, gin.H{"error": err.Error()})
		} else {
			c.JSON(500, gin.H{"error": err.Error()})
		}
		return
	}
	// 返回
	c.JSON(201, resp)
}
